thin.imports('/public/configure/conf.complete.js');
thin.imports('/public/plug/laytpl/laytpl.js');

thin.use(function() {
	thin.imports('/public/webfe/resource/sms.js');

	$('#btn-checkaccess').on('click', function() {
		var that = $(this),
			action = that.data('action');

		var data_opt = {
			'mobile': $('[layout-mark=sms-mobile]').val(),
			'model': 1
		};

		if(data_opt.mobile == '') {
			layer.msg('请输入手机号', {
				icon: 2,
				time: 800
			});
			return;
		}

		layer.load(0, {
			shade: [0.08, '#000']
		});

		if(action == 'bind') {
			__getdata_wxbind = data_opt;
			verifySMSCode(function() {
				factory.request('_reg_wxbind');
			});
		} else {
			__getdata_wxreg = data_opt;
			verifySMSCode(nextViden);
		}

		/*var data_opt = {
			'mobile': $('[layout-mark=sms-mobile]').val(),
			'password': $('#ipt-su-password').val(),
			'confpwd': $('#ipt-su-confpwd').val()
		};*/

		/*if(action == 'bind') {
			layer.load(0, {
				shade: [0.08, '#000']
			});
			__getdata_wxbind['mobile'] = data_opt['mobile'];
			__getdata_wxbind['model'] = 1;
			bindAcc();
		} else if(action == 'register') {
			if(data_opt.password == '') {
				layer.msg('请输入密码', {
					icon: 2,
					time: 800
				});
				return;
			} else if(data_opt.confpwd != data_opt.password) {
				layer.msg('两次密码输入不正确', {
					icon: 2,
					time: 800
				});
				return;
			}
			layer.load(0, {
				shade: [0.08, '#000']
			});
			__getdata_wxreg = data_opt;
			__getdata_wxreg['model'] = 1;
			nextViden();
		}*/
	});

	$('#btn-selone').on('click', function() {
		data_role = 1;
		submitSingUp();
	});

	$(function() {
		$('#btn-seliden').on('click', function() {
			svt_index = layer.open({
				type: 1,
				skin: 'layui-layer-demo', //样式类名
				area: ['480px', 'auto'],
				closeBtn: 1,
				anim: 2,
				shadeClose: true,
				content: $('#selwin-viden-template').html()
			});
		});
	});

	$(document).on('click', '#btn-pers', function() {
		layer.close(svt_index);
		data_role = 2;
		submitSingUp();
	}).on('click', '#btn-mech', function() {
		layer.close(svt_index);
		data_role = 3;
		nextMechanism();
	}).on('click', '.mlist-item', function() {
		__getdata_wxreg['organization'] = $(this).attr('data-title');
		$(this).addClass('active').siblings('a').removeClass('active');
	}).on('click', '#btn-seljg', function() {
		submitSingUp();
	}).on('click', '#btn-addjg', function() {
		layer.close(svt_index);
		nextAddMech();
	}).on('click', '#btn-canceljg', function() {
		layer.close(svt_index);
	}).on('click', '#submit-addmech', function() {
		submitAddmech();
	}).on('click', '#btn-cancelreg', function() {
		layer.close(svt_index);
	}).on('click', '#submit-regis', function() {
		//layer.close(svt_index);
		submitRegister();
	});
});

var svt_index,
	data_role,
	data_mechanism = [],
	__getdata_orgvolist = {},
	__getdata_wxbind = {},
	__getdata_wxreg = {},
	__getdata_orgadd = {};

function __complete_wxbind(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('绑定成功');
		setTimeout(function() {
			window.location.href = '/';
		}, 1200);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

function nextViden() {
	layer.closeAll('loading');
	$('.sing-template').css('display', 'none');
	$('.rb-viden').fadeIn(96);
}

function nextMechanism() {
	if(data_mechanism.length <= 0) {
		layer.load(0, {
			shade: [0.08, '#000']
		});
		factory.request('_org_volist');
	} else {
		showMechanism(data_mechanism);
	}
}

function __complete_orgvolist(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		data_mechanism = res.data;
		showMechanism(res.data);
	} else {

	}
}

function showMechanism(data) {
	var template = $('#selwin-mlist-template').html(),
		tpl = laytpl(template);
	svt_index = layer.open({
		type: 1,
		area: ['368px', 'auto'],
		closeBtn: 0,
		title: '选择投资机构',
		anim: 2,
		shadeClose: true,
		content: tpl.render(data)
	});
}

function nextAddMech() {
	svt_index = layer.open({
		type: 1,
		area: ['400px', 'auto'],
		closeBtn: 0,
		title: '新增投资机构',
		anim: 2,
		shadeClose: true,
		content: $('#selwin-addjg-template').html()
	});
}

function submitAddmech() {
	var data_opt = {
		'logo': $('#ipt-jg-logo').val(),
		'title': $('#ipt-jg-title').val(),
		'introduction': $('#ipt-jg-introduction').val()
	};
	if(data_opt.title == '') {
		layer.msg('请输入机构名称', {
			icon: 2,
			time: 800
		});
		return;
	} else if(data_opt.introduction == '') {
		layer.msg('请输入机构简介', {
			icon: 2,
			time: 800
		});
		return;
	}
	layer.close(svt_index);
	layer.load(0, {
		shade: [0.08, '#000']
	});
	__getdata_orgadd = data_opt;
	factory.request('_org_add', data_opt.title);
}

function __complete_orgadd(res, unkn) {
	layer.closeAll('loading');
	__getdata_wxreg['organization'] = unkn;
	submitSingUp();
}

function submitSingUp() {
	//layer.close(svt_index);
	svt_index = layer.open({
		type: 1,
		area: ['400px', 'auto'],
		closeBtn: 0,
		title: '设置密码',
		anim: 2,
		shadeClose: true,
		content: $('#selwin-supwd-template').html()
	});
}

function submitRegister() {
	var data_opt = {
		'password': $('#ipt-su-password').val(),
		'confpwd': $('#ipt-su-confpwd').val()
	}
	if(data_opt.password == '') {
		layer.msg('请输入密码', {
			icon: 2,
			time: 800
		});
		return;
	} else if(data_opt.confpwd != data_opt.password) {
		layer.msg('两次密码输入不正确', {
			icon: 2,
			time: 800
		});
		return;
	}
	layer.load(0, {
		shade: [0.08, '#000']
	});
	__getdata_wxreg = tool.optionExtend(__getdata_wxreg, data_opt);
	__getdata_wxreg['role'] = data_role;

	factory.request('_reg_wxreg');
}

function __complete_wxreg(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('绑定成功');
		setTimeout(function() {
			window.location.href = '/';
		}, 1200);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}